package co.fitstart.weapp.api.web.controller.profile;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;

import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.user.User;
import co.fitstart.entity.utils.StringUtils;
import co.fitstart.weapp.api.service.CouponService;
import co.fitstart.weapp.api.service.UserService;

/**
 * 
 * @author dan
 *
 */
@RestController
@RequestMapping("/profile")
public class ProfileEditController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private CouponService couponService;
    
    private static final Logger logger = LoggerFactory.getLogger(ProfileEditController.class);
    
    /**
     * 
     * @param unionid
     * @return
     */
    @RequestMapping(path = "/shipping-address", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult addressEdit(@RequestParam("unionid") String unionid) {
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            
            httpResult.success().message("获取数据成功")
                                .data("shippingAddress", user.getShippingAddress());
            
            
        } catch (Exception e) {
            
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }
    
    /**
     * 
     * @param unionid
     * @param name
     * @param mobile
     * @param region
     * @param address
     * @return
     */
    @RequestMapping(path = "/shipping-address", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult onAddressEdit(@RequestParam("unionid") String unionid,
                                    @RequestParam("name") String name,
                                    @RequestParam("mobile") String mobile,
                                    @RequestParam("region") String region,
                                    @RequestParam("address") String address) {
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            userService.editAddress(name, mobile, region, address, user);
            httpResult.success().message("修改收货地址成功");
            
        } catch (Exception e) {
            logger.error("onAddressEdit - ", e);
            httpResult.fail("500").message("修改收货地址失败");
        }
        
        return httpResult;
    }
    
    /**
     * 
     * @param unionid
     * @return
     */
    @RequestMapping(path = "/region", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult regionEdit(@RequestParam("unionid") String unionid) {
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            
            httpResult.success().message("获取数据成功")
                                .data("user", user);
            
            
        } catch (Exception e) {
            
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }
    
    /**
     * 
     * @param unionid
     * @param country
     * @param prd
     * @return
     */
    @RequestMapping(path = "/region", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult onRegionEdit(@RequestParam("unionid") String unionid,
                                   @RequestParam("country") String country,
                                   @RequestParam("prd") String prd) {
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            userService.editRegion(country, prd, user);
            httpResult.success().message("修改地区成功");
            
        } catch (Exception e) {
            logger.error("onRegionEdit - ", e);
            httpResult.fail("500").message("修改地区失败");
        }
        
        return httpResult;
    }
    
    /**
     * 
     * @param unionid
     * @param couponId
     * @param mobile
     * @return
     */
    @RequestMapping(value = "/coupon/{couponId}/give", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult onGiveCoupon(@RequestParam("unionid") String unionid,
                                   @PathVariable("couponId") Long couponId,
                                   @RequestBody Map<String, String> params) {
        HttpResult httpResult = new HttpResult();
        
        try {
            User user = userService.load(unionid);
            Coupon coupon = couponService.load(couponId);
            
            couponService.give(coupon, user, params.get("name").trim(), params.get("mobile"));
            
            httpResult.success().message("转赠成功");
        } catch (BusinessException e) {
            httpResult.fail("500").message(e.getMessage());
        } catch (Exception e) {
            logger.error("onGiveCoupon() error", e);
            httpResult.fail("500").message("转赠失败");
        }
        
        return httpResult;
    }

    @RequestMapping(value = "/edit-mobile", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult onEditMobile(@RequestParam("unionid") String unionid,
                                   @RequestParam("mobile") String mobile) {
        HttpResult httpResult = new HttpResult();
        
        try {
            User user = userService.load(unionid);
            
            if(user == null || StringUtils.isBlank(mobile)) {
                httpResult.fail("500").message("请求参数有误");
            } else {
                userService.editMobile(user, mobile.replace(" ", ""));
                
                httpResult.success().message("修改手机号成功");
            }
        } catch (BusinessException e) {
            httpResult.fail("500").message(e.getMessage());
        } catch (Exception e) {
            logger.error("onEditMobile() error", e);
            httpResult.fail("500").message("修改手机号失败");
        }
        
        return httpResult;
    }
}
